package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.KjtInvokeHisDTO;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.KjtInvokeHisEntity;
import com.cloudfun.campusshare.repo.KjtInvokeHisRepo;
import com.cloudfun.campusshare.service.KjtInvokeHisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by CodeGenerator on 2020/08/11
 */
@Slf4j
@Service
public class KjtInvokeHisServiceImpl implements KjtInvokeHisService {

    @Autowired
    private KjtInvokeHisRepo kjtInvokeHisRepo;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void create(KjtInvokeHisDTO invokeHisDTO) {
        KjtInvokeHisEntity invokeHisEntity = kjtInvokeHisRepo.findByRequestNo(invokeHisDTO.getRequestNo());
        if (null != invokeHisEntity) {
            throw new BusinessException(Code.DATA_EXIST);
        }
        invokeHisEntity = new KjtInvokeHisEntity();
        BeanUtil.copyProperties(invokeHisDTO, invokeHisEntity);
//        invokeHisEntity.setUserId(AuthenticationHolder.getUserId());
        invokeHisEntity.setUserId("123456");
        kjtInvokeHisRepo.save(invokeHisEntity);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void modify(KjtInvokeHisDTO kjtInvokeHisDTO) {
        KjtInvokeHisEntity invokeHisEntity = kjtInvokeHisRepo.findByRequestNo(kjtInvokeHisDTO.getRequestNo());
        BeanUtil.copyProperties(kjtInvokeHisDTO, invokeHisEntity, CopyOptions.create().setIgnoreNullValue(true));
        kjtInvokeHisRepo.save(invokeHisEntity);
    }
}